package com.ruoyi.web.controller.gwc;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TCostRecord;
import com.ruoyi.gwc.service.ITCostRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用记录Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/costRecord")
public class TCostRecordController extends BaseController
{
    @Autowired
    private ITCostRecordService tCostRecordService;

    /**
     * 查询费用记录列表
     */
    @ApiOperation("查询费用记录列表")
    @GetMapping("/list")
    public TableDataInfo list(TCostRecord tCostRecord)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tCostRecord.getCompanyId()))) {
            tCostRecord.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TCostRecord> list = tCostRecordService.selectTCostRecordList(tCostRecord);
        for (TCostRecord costRecord : list ){
            if(ObjectUtils.isNotEmpty(costRecord.getAmount())) {
                costRecord.setAmountStr(costRecord.getAmount().toString());
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出费用记录列表
     */
    @Log(title = "费用记录", businessType = BusinessType.EXPORT)
    @ApiOperation("导出费用记录列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCostRecord tCostRecord)
    {
        List<TCostRecord> list = tCostRecordService.selectTCostRecordList(tCostRecord);
        ExcelUtil<TCostRecord> util = new ExcelUtil<TCostRecord>(TCostRecord.class);
        util.hideColumn("remark");
        util.exportExcel(response, list, "费用记录数据");
    }

    /**
     * 获取费用记录详细信息
     */
    @ApiOperation("获取费用记录详细信息")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(tCostRecordService.selectTCostRecordByRecordId(recordId));
    }

    /**
     * 新增费用记录
     */
    @Log(title = "费用记录", businessType = BusinessType.INSERT)
    @ApiOperation("新增费用记录")
    @PostMapping
    public AjaxResult add(@RequestBody TCostRecord tCostRecord)
    {
        tCostRecord.setAmount(new BigDecimal(tCostRecord.getAmountStr()));
        return toAjax(tCostRecordService.insertTCostRecord(tCostRecord));
    }

    /**
     * 修改费用记录
     */
    @Log(title = "费用记录", businessType = BusinessType.UPDATE)
    @ApiOperation("修改费用记录")
    @PutMapping
    public AjaxResult edit(@RequestBody TCostRecord tCostRecord)
    {
        tCostRecord.setAmount(new BigDecimal(tCostRecord.getAmountStr()));
        return toAjax(tCostRecordService.updateTCostRecord(tCostRecord));
    }

    /**
     * 删除费用记录
     */
    @Log(title = "费用记录", businessType = BusinessType.DELETE)
    @ApiOperation("删除费用记录")
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(tCostRecordService.deleteTCostRecordByRecordIds(recordIds));
    }
}
